package cn.blog.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.blog.dto.SystemUserAdd;
import cn.blog.dto.SystemUserLogin;
import cn.blog.entity.SystemUser;
import cn.blog.service.SystemUserService;
import cn.triedblog.LayuiTableResult;
import cn.triedblog.PublicResultJosn;
import cn.triedblog.utils.AppUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 后台用户接口
 * 
 * @author 偶尔有点困
 * @date 2018年5月4日
 */
@Api(tags = "后台用户")
@RequestMapping("/sysUser")
@RestController
public class SystemUserController {

	@Autowired
	private SystemUserService systemUserService;

	@PostMapping
	@ApiOperation(value = "添加用户")
	public PublicResultJosn add(@RequestBody SystemUserAdd user_add) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.add(user_add);
		return resultJosn;
	}

	@DeleteMapping
	@ApiOperation(value = "单、批量删除用户")
	public PublicResultJosn delete(@RequestBody List<String> deleteids) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.delete(deleteids);
		return resultJosn;
	}

	@PutMapping
	@ApiOperation(value = "更新用户信息")
	public PublicResultJosn update(@RequestBody SystemUserAdd user) {
		PublicResultJosn resultJosn = null;
		resultJosn = systemUserService.updateByExample(user);
		return resultJosn;
	}

	@GetMapping
	@ApiOperation(value = "用户列表条件模糊查询")
	public LayuiTableResult selectByExample(SystemUser user, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize) {
		LayuiTableResult resultJosn = systemUserService.selectByExample(user, page, pageSize);
		return resultJosn;
	}

	@GetMapping(value = "/{uid}")
	@ApiOperation(value = "根据uid查找、包括所拥有的角色")
	public PublicResultJosn selectByUid(@PathVariable String uid) {
		PublicResultJosn resultJosn = systemUserService.selectByUid(uid);
		return resultJosn;
	}

	@GetMapping(value = "/internal/get_user_info")
	@ApiOperation(value = "根据username查找用户、包括角色、权限（仅限于微服务内部调用）")
	public SystemUserLogin selectByUsername(@RequestParam String username) {
		if (StringUtils.isBlank(username)) {
			throw new IllegalArgumentException("username参数错误");
		}
		SystemUserLogin resultJosn = systemUserService.selectByUsername(username);
		return resultJosn;
	}

	@GetMapping(value = "/current")
	@ApiOperation(value = "获取当前登录用户信息")
	public cn.triedblog.dto.SystemUserLogin getLoginUserInfo() {
		return AppUserUtil.getLoginAppUser();
	}

}
